/**
 * Created with IntelliJ IDEA.
 * User: gg
 * Date: 14-5-4
 * Time: 下午4:49
 * To change this template use File | Settings | File Templates.
 */
$.fn.extend({
        initTable: function (params) {
            params = $.dealDefault(params, {});
            var url = $.dealDefault(params.url, '');
            var realUrl = url;
            if (!$.checkUndefined(params.pagination)) {
                var pagination = params.pagination;
                realUrl += '?page=' + pagination.page + '&rows=' + pagination.rows;
            }
            var tableId = $(this).attr('id');
            $('#' + tableId).load(realUrl, function () {
                $(this).find('table').attr('url', url);
                $(this).find('table').attr('tableId', tableId);
                var allPage = parseInt($('#allPage').val());
                var currentPage = parseInt($('#currentPage').attr('page'));
                var next = currentPage + 1;
                var pre = currentPage - 1;
                var first = 1;
                var last = allPage;
                var prePage = $('.pagination').first().find('.prePage').first();
                var nextPage = $('.pagination').first().find('.nextPage').first();
                var firstPage = $('.pagination').first().find('.firstPage').first();
                var lastPage = $('.pagination').first().find('.lastPage').first();
                if (currentPage == '1' && currentPage != allPage) {
                    prePage.parents('li').addClass('disabled');
                    firstPage.parents('li').addClass('disabled');
                    pre = currentPage;
                } else if (currentPage == '1' && currentPage == allPage) {
                    prePage.parents('li').addClass('disabled');
                    firstPage.parents('li').addClass('disabled');
                    pre = currentPage;

                    lastPage.parents('li').addClass('disabled');
                    nextPage.parents('li').addClass('disabled');
                    next = currentPage;
                } else if (currentPage == allPage) {
                    lastPage.parents('li').addClass('disabled');
                    nextPage.parents('li').addClass('disabled');
                    next = currentPage;
                }
                $('.firstPage').click(function (e) {
                    var disabled = $(this).parents('li').attr('class').indexOf('disabled');
                    if (disabled >= 0) {
                        return false;
                    }
                    var url = $(this).parents('.row').find('table').first().attr('url');
                    var tableId = $(this).parents('.row').find('table').first().attr('tableId');
                    $('#' + tableId).initTable({
                        url: url,
                        pagination: {page: first, rows: 10}
                    })
                    e.stopPropagation();
                })

                $('.prePage').click(function (e) {
                    var disabled = $(this).parents('li').attr('class').indexOf('disabled');
                    if (disabled >= 0) {
                        return false;
                    }
                    var url = $(this).parents('.row').find('table').first().attr('url');
                    var tableId = $(this).parents('.row').find('table').first().attr('tableId');
                    $('#' + tableId).initTable({
                        url: url,
                        pagination: {page: pre, rows: 10}
                    })
                    e.stopPropagation();
                })

                $('.nextPage').click(function (e) {
                    var disabled = $(this).parents('li').attr('class').indexOf('disabled');
                    if (disabled >= 0) {
                        return false;
                    }
                    var url = $(this).parents('.row').find('table').first().attr('url');
                    var tableId = $(this).parents('.row').find('table').first().attr('tableId');
                    $('#' + tableId).initTable({
                        url: url,
                        pagination: {page: next, rows: 10}
                    })
                    e.stopPropagation();
                })

                $('.lastPage').click(function (e) {
                    var disabled = $(this).parents('li').attr('class').indexOf('disabled');
                    if (disabled >= 0) {
                        return false;
                    }
                    var url = $(this).parents('.row').find('table').first().attr('url');
                    var tableId = $(this).parents('.row').find('table').first().attr('tableId');
                    $('#' + tableId).initTable({
                        url: url,
                        pagination: {page: last, rows: 10}
                    })
                    e.stopPropagation();
                })
            })
        },
        getTableSelectedIds: function () {
            var array = new Array();
            $(this).find('input:checkbox[name=id]:checked').each(function () {
                array.push($(this).val());
            });
            return array;
        }
})